// pages/fen_lei/fen_lei.js
import {request} from "../../utils/http.js"


Page({

  /**
   * 页面的初始数据
   */
  data: {
    value:"",
    fenlei:[
      "为你推荐",
      "水果区",
      "水果区",
      "水果区",
      "水果区",
      "水果区",
      "水果区",
      "水果区",
      "水果区",
      "水果区",
      "水果区",
      "水果区",
      "水果区",
      "水果区",
      "水果区",
      "水果区"
    ],
    xuanzhong:1,
    goods1:[
      {
        url:"http://122.51.84.231:9001/dianshang/鸡蛋.png",
        text:"语农 散装土鸡蛋 360枚 40斤",
        time:"08月13日",
        money:"28.8",
        nums:2700
      },
      {
        url:"http://122.51.84.231:9001/dianshang/鸡蛋.png",
        text:"语农 散装土鸡蛋 360枚 40斤",
        time:"08月13日",
        money:"28.8",
        nums:2700
      },
      {
        url:"http://122.51.84.231:9001/dianshang/鸡蛋.png",
        text:"语农 散装土鸡蛋 360枚 40斤",
        time:"08月13日",
        money:"28.8",
        nums:2700
      },
    ],
  },
  gobuy(e){
    request({
      url:"/getInfo",
      header: {
        "authorization": getApp().globalData.authorization
      }
    }).then(data=>{
      console.log(data);
      if(data.code==200){
        request({
          url:"/dianshang/cart",
          method:"POST",
          data:{
            goodsid:this.data.goods1[e.currentTarget.id].id,
            nums:1,
            userid:getApp().globalData.userInfo.userId,
            del:0
          },
          header: {
            "authorization": getApp().globalData.authorization
          }
        }).then(data1=>{
          if(data1.code==200){
            wx.showModal({
              title: data.msg,
              showCancel: false
            });
            getApp().globalData.nums++;
            wx.setTabBarBadge({
              index:2,
              text:getApp().globalData.nums.toString(),
            })
          }
        })
      }else{
        wx.navigateTo({
          url: '/pages/deng_lu/deng_lu',
        })
      }
    })
   
  },
  close(){
    this.setData({
      show:false
    })
  },
  to(res){
    wx.navigateTo({
      url: res.currentTarget.id,
    })
  },
  tobar(e){
    wx.reLaunch({
      url:e.currentTarget.id
    })
  },
  onChange(e){
    console.log(e.detail);
    console.log(this.data);
    this.setData({
      value:e.detail
    });
    if(this.data.xuanzhong==undefined||this.data.xuanzhong==1){
      request({
        url:"/dianshang/goods/list?name="+e.detail
      }).then((data)=>{
        console.log(data);
        this.setData({
          goods1:data.rows
        })
      })
    }else{
      request({
        url:"/dianshang/goods/list?gc="+this.data.xuanzhong+"&name="+e.detail
      }).then((data)=>{
        this.setData({
          goods1:data.rows
        })
      })
    }

  },
  to(e){
    request({
      url: "/getInfo",
      header: {
        "authorization": getApp().globalData.authorization
      }
    }).then((data)=>{
      if (data.code == 200) {
        console.log(e.currentTarget.id);
        wx.navigateTo({
          url: "/pages/xiang_qing/xiang_qing?id="+e.currentTarget.id,
        })
      }
      if (data.code == 401) {
        wx.navigateTo({
          url: '/pages/deng_lu/deng_lu',
        })
      }
    })   
  },
  click(e){
    if(this.data.value==undefined)this.data.value="";
    console.log(e);
    this.setData({
      xuanzhong:e.currentTarget.id
    })
    if(e.currentTarget.id==1){
      request({
        url:"/dianshang/goods/list?name="+this.data.value
      }).then((data)=>{
        this.setData({
          goods1:data.rows
        })
      })
    }else{
      request({
        url:"/dianshang/goods/list?gc="+e.currentTarget.id+"&name="+this.data.value
      }).then((data)=>{
        this.setData({
          goods1:data.rows
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    console.log(this.data);
    this.setData({
      xuanzhong:options.id,
      value:options.value
    })
    if(options.value==undefined)options.value="";
    request({
      url: "/system/fl/list"
    }).then((data) => {
      this.setData({
        fenlei: data.rows
      })
    })
    if(options.id==undefined || options.id==1){
      request({
        url:"/dianshang/goods/list?name="+options.value
      }).then((data)=>{
        this.setData({
          goods1:data.rows
        })
      })
    }else{
   request({
      url:"/dianshang/goods/list?gc="+options.id+"&name="+options.value
    }).then((data)=>{
      this.setData({
        goods1:data.rows
      })
    })
    }
 

  

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})